<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-07-08
 * Time: 15:35
 */

namespace app\api\service\money;


use app\api\model\MdbUser;
use app\api\model\MoneyOrder;
use app\api\model\User;
use app\api\model\UserMoney;
use app\api\model\WechatUser;
use app\api\service\Token;
use app\api\validate\CheckPhone;
use app\lib\exception\ParameterException;
use think\Db;
use think\Exception;

class UpdateUserMoneySer extends Token
{
    /**
     * 根据手机号和密码查询微信用户信息
     * 没有返回没有绑定信息
     * 有 1.添加余额记录  2.更新用户余额以及更新微信用户的openid 3.更新微信用户的余额以及状态
     */

    // 小程序用户信息
    private $user;

    // 传过来的参数
    private $postArr;
    
    // 微信用户信息
    private $wechatUser;

    // 微信用户一
    private $wechatUserOne;


    function __construct($post)
    {
        $this->postArr = $post;
        $uid = self::getCurrentUid();
        $this->user = User::get($uid);
    }

    // 入口方法
    public function entrance()
    {
        return $this->judgePhone();
    }

    // 判断传入信息
    public function judgePhone()
    {
        (new CheckPhone())->goCheck();
        $this->wechatUser = MdbUser::where('phone',$this->postArr['phone'])
            ->where('password',$this->postArr['password'])
            ->find();
        if(!$this->wechatUser) {
            throw new ParameterException([
                'msg' => [
                    'user_money' => '此信息未绑定'
                ]
            ]);
        }
        if($this->wechatUser['status'] == 2) {
            throw new ParameterException([
                'msg' => [
                    'user_money' => '信息已经同步'
                ]
            ]);
        }
        $this->wechatUserOne = WechatUser::where('id',$this->wechatUser['id'])->find();
        if(!$this->wechatUserOne) {
            User::where('id',$this->user['id'])->update([
                'wechat_openid' => $this->wechatUser['openid'],
                'phone' => $this->wechatUser['phone'],
                'password' => $this->wechatUser['password'],
            ]);
            MdbUser::where('id',$this->wechatUser['id'])->update([
                'status' => 2
            ]);
            return writeJson(202, [], '数据同步成功！');
        }
        return $this->updateData();
    }

    // 更新用户信息
    private function updateData()
    {
        Db::startTrans();
        try {
            $this->updateMoney();
            Db::commit();
            return writeJson(202, [], '数据同步成功！');
        }
        catch (Exception $ex)
        {
            Db::rollback();
            throw $ex;
        }
    }

    // 更新操作
    private function updateMoney()
    {
        if($this->wechatUser['money'] > 0) {
            UserMoney::create([
                'money' => $this->wechatUser['money'],
                'user_id' => $this->user['id'],
                'type' => 6,
                'user_money' => $this->user['money']
            ]);
            $userMoney = round($this->user['money']+$this->wechatUser['money'],2);
            User::where('id',$this->user['id'])->update([
                'money' => $userMoney
            ]);
        }
        if(!$this->wechatUserOne['status']) {
            $this->wechatUserOne['status'] = '普通会员';
        }
        User::where('id',$this->user['id'])->update([
            'wechat_openid' => $this->wechatUser['openid'],
            'phone' => $this->wechatUser['phone'],
            'password' => $this->wechatUser['password'],
            'grade_name' => $this->wechatUserOne['status'],
            'user_no' => $this->getUserNo($this->wechatUserOne['title']),
        ]);
        MdbUser::where('id',$this->wechatUser['id'])->update([
            'money' => 0,
            'status' => 2
        ]);
    }


    private function getUserNo($user_no)
    {
        $number = 6 - strlen($user_no);
        if($number > 0) {
            return getRandNumber0($number).$user_no;
        } else {
            return $user_no;
        }
    }
}